<?php

namespace Btr\DurakBundle\Controller;

// use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContextInterface;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Btr\DurakBundle\EventListener\InitializableControllerInterface;
use Btr\DurakBundle\Durak\Durak;

class DefaultController extends Controller implements InitializableControllerInterface
{
	/**
	 * It contains all data represents the game.
	 * @var Durak
	 */
	private $game;
	
	/**
	 * (non-PHPdoc)
	 * @see \Btr\DurakBundle\EventListener\InitializableControllerInterface::initialize()
	 */
	public function initialize(Request $request, SecurityContextInterface $security_context)
	{
		$route = $request->get('_route');
		if ( $route == 'btr_durak_new')
		{
			$this->game = null;
			return;
		}
		$this->game = Durak::retrieveGame();
	}
	
	/**
	 * Home page
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
    public function indexAction()
    {
        return $this->render('BtrDurakBundle:Default:index.html.twig');
    }

    /**
     * Create a new game.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function newAction()
    {
    	$game = new Durak();
    	$game->saveGame();
    	return new Response(count($game->getCards()),200);
    }
    
    /**
     * Start the game.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function startAction()
    {
    	$request = $this->getRequest()->request;
    	$auto = $request->get('auto');
    	$result = $this->game->start($auto);
    	$this->game->saveGame();
		return new Response(json_encode($result),200);
    }
    
    /**
     * Try to attack.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function tryAttackAction()
    {
    	$result = $this->game->getGameDone();
    	if ( ! $result['done'])
    	{ 
	    	$attacker = $this->getRequest()->request->get('attacker');
	    	$result = array_merge($result,$this->game->tryAttack($attacker));
	    	$this->game->saveGame();
    	}
    	return new Response(json_encode($result),200);
    }

    /**
     * Try to defend.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function tryDefendAction()
    {
    	$request = $this->getRequest()->request;
    	$attackCard = $request->get('attackCard');
    	$defender = $request->get('defender');
    	$result = $this->game->tryDefend($defender,$attackCard);
    	$this->game->saveGame();
    	return new Response(json_encode($result),200);
    }

    /**
     * A attack is done.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function attackAction()
    {
    	$request = $this->getRequest()->request;
    	$attacker = $request->get('attacker');
    	$attackCard = $request->get('attackCard');
    	 
    	$result = $this->game->attack($attacker, $attackCard);
    	$this->game->saveGame();
    	return new Response(json_encode($result),200);
    }
    
    /**
     * A defense is done.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function defendAction()
    {
    	$request = $this->getRequest()->request;
    	$defendCard = $request->get('defendCard');
    	$defender = $request->get('defender');
    	$result = $this->game->defend($defender,$defendCard);
    	$this->game->saveGame();
    	return new Response(json_encode($result),200);
    }
    
    /**
     * Stop attack
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function endAttackAction()
    {
    	$request = $this->getRequest()->request;
    	$attacker = $request->get('attacker');
    	$result = $this->game->endAttack($attacker);
    	$this->game->saveGame();
    	return new Response(json_encode($result),200);    	    	
    }
    
    /**
     * Check auto-accept for the player.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function checkAutoAcceptAction()
    {
    	$request = $this->getRequest()->request;
    	$defender = $request->get('defender');
    	$result = $this->game->checkAutoAccept($defender);
    	$this->game->saveGame();
    	return new Response(json_encode($result),200);
    }
    
    /**
     * Accept cards when can not defend.
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function acceptAction()
    {
    	$request = $this->getRequest()->request;
    	$defender = $request->get('defender');
    	$result = $this->game->accept($defender);
    	$this->game->saveGame();
    	return new Response(json_encode($result),200);
    }
   
}
